<?php
	define('PHP_PATH', 'C:/crm/wamp/bin/php/php5.2.6/php.exe');
	define('SEND_PATH','C:/crm/wamp/www/phpmail/ajaxmail/send_port.php');
	date_default_timezone_set("Etc/GMT-8");
	ini_set('max_execution_time',30000);
	//error_reporting(E_ERROR);
	include_once 'function.php';
	include_once 'db_config.php';
	$fun = getRequest('callback');
	//exit(retjson(1,$_SESSION['username'],$fun));
	/*  跨域问题后面解决
	if(empty($_SESSION['username'] )){
		exit(retjson(0,'unlogin', $fun));
	}
	*/
	$username = getRequest('senduser');
	$mailgroupid = getRequest('groupid');
	$sendgroup = getRequest('sendgroup');
	$delaytime = getRequest('delay',0)*1000;
	$minms = getRequest('minms',0)*1000;
	$maxms = getRequest('maxms',0)*1000;
	$sendn = getRequest('sendn',0)*1000;
	$stopm = getRequest('stopm',0)*1000;
	$content = urldecode(getRequest('content'));
	$subject = urldecode(getRequest('obj'));
	
	if(empty($content) || empty($subject)||empty($mailgroupid)){
		exit(retjson(0,'Content not null!',$fun));
	}
	//exit(retjson(0,$content));
	if(empty($username)){
		exit(retjson(0,'unlogin', $fun));
	}
	try{
		$conn = @mysql_connect($dbhost,$dbuser,$dbpwd) or die(retjson(0,'mysql_connect error!'));
		@mysql_select_db($dbname) or die(retjson(0,'mysql_select_db error!'));
		@mysql_query('set names utf8') or die(retjson(0,'mysql_query error!'));
		$sql = 'insert into `crm_smtplog` values("","'.$mailgroupid.'","'.$subject.'","'.addslashes($content).'","'.$username.'",now(),0)';
		@mysql_query($sql);
		$send_id = mysql_insert_id();
		mysql_close($conn);
	}catch(Exception $e){
		writelog($e,__FILE__,__LINE__);
	}
	//通过脚本发送邮件
	try{
		//echo PHP_PATH.' '.SEND_PATH." {$mailgroupid} {$sendgroup} {$delaytime} {$send_id} ";exit;
		$fp = popen(PHP_PATH.' '.SEND_PATH." {$mailgroupid} {$sendgroup} {$delaytime} {$send_id} {$minms} {$maxms} {$sendn} {$stopm} &",'r');
		/*while(!feof($fp)){
			$sendret = fgets($fp);
		}*/
		pclose($fp);
	}catch(Exception $e){
		writelog($e,__FILE__,__LINE__);
		//exit(0,substr($e,1));
	}
	
	exit(isset($sendret)?$sendret:retjson());
	/*
	$_SESSION['username'] = !empty($username)?$username:'undefined';

	$conn = mysql_connect('localhost','root','');
	mysql_select_db('admin_crm');
	mysql_query('set names utf8');
	//初始化fromEmail当天发送数为0
	$sql = 'update `crm_smtpsendermaillist` set `daytimes`=0 where `senddate`<>"'.date('Y-m-d',time()).'"';
	mysql_query($sql);
	//得到接收邮件清单
	$sql = 'select `mail_sendmail` mail,`user_name` name from `crm_smtpsendmalilist` where `send_state` in (2,3) and  `mailgroup_id` in ('.$mailgroupid.')';
	$ret = @mysql_query($sql);
	$maillist = array();
	$i=0;
	while ($row = @mysql_fetch_array($ret)){
		$maillist[$i]['name'] = $row['name'];
		$maillist[$i++]['mail'] = $row['mail'];
	}
	if(count($maillist)<1){
		exit(retjson(0,'Email Group No Emails!',$fun));
	}
	//插入发送记录
	$sql = 'insert into `crm_smtplog` values("","'.$mailgroupid.'","'.$subject.'","'.addslashes($content).'","'.$_SESSION['username'].'",now(),0)';
	@mysql_query($sql);
	$send_id = mysql_insert_id();
	//$sendret = doGroupSending($maillist,$subject,$content,$send_id,$sendgroup,$delaytime);
	try{
		echo PHP_PATH.' '.SEND_PATH." {$maillist} {$subject} {$content} {$send_id} {$sendgroup} {$delaytime}";
		$fp = popen(PHP_PATH.' '.SEND_PATH." {$maillist} {$subject} {$content} {$send_id} {$sendgroup} {$delaytime}",'r');
		while(!feof($fp)){
			$sendret = fgets($fp);
		}
		pclose($fp);
	}catch(Exception $e){
		writelog($e);
	}
	mysql_close($conn);
	*/
	//var_dump($sendret);
?>